import datetime
from db import db
from util.myredis import r
from util.e_email import emailys
from util.sms import send_message
from views.base import BaseHandler
from util.e_email import emailys
class Duedata(BaseHandler):
    def get(self):
        pass


def duedata():
    # 一周后的今天
    date1 = datetime.datetime.now()+datetime.timedelta(days=7)
    # 格式化年月日
    nitme = datetime.datetime.strftime(date1,"%Y-%m-%d")
    print(date1)
    sql = "select code,userid from (select * from landmarks1 where status=4 union all select * from landmarks2 where status=4 union all select * from landmarks where status=4)t where date_format(t.due_time,'%Y-%m-%d') ='"+nitme+"'"
    res = db.findall(sql)
    if res:
        for i in res:
            sql1 = "select * from pcuser where id=%d"%(i['userid'])
            user = db.findone(sql1)
            key = "youxiang" + str(i['code'])
            red = r.str_get(key)
            if not red:
                emailys(user['emelits'])
                r.str_add(key, user['emelits'])
            else:
                print(user['mobile'], '已发送用户邮箱')

        # emailys()
    # 还款20
    # 现在23


def withhold():
    now = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d")
    sql = "select code,userid from (select * from landmarks1 where status=4 union all select * from landmarks2 where status=4 union all select * from landmarks where status=4)t where date_format(t.due_time,'%Y-%m-%d') ='"+now+"'"
    res = db.findall(sql)
    print('res',res)
    if res:
        for i in res:
            # 查找所有投标的人
            sql1 = "select * from invest where landmarksid='%s'"%(i['code'])
            user = db.findall(sql1)
            for j in user:
                # 更新所有用户投资的钱
                sql3 = "select * from pcuser where id=%d" % (j['userid'])
                pcc= db.findone(sql3)
                qian = pcc['money']+j['tmoney']
                sql2 = "update pcuser set money='%s' where id=%d"%(qian,int(j['userid']))
                print(sql2)
                db.update(sql2)

                # 减还款的金额
                sql5 = "select * from pcuser where id=%d"%(i['userid'])
                pc = db.findone(sql5)
                money = pc['money'] - j['tmoney']
                print(money)
                sql4 = "update pcuser set money='%s' where id=%d"%(money,int(i['userid']))
                db.update(sql4)
                number = hash(i['code']) % 3
                tablename = 'landmarks'
                status = 6
                if number > 0:
                    tablename = tablename + str(number)
                sql6 = "update " + tablename + " set status=%d where code=%d" % (status, i['code'])
                db.update(sql6)
                # db.commit()
            print('还款完成')
            return '还款完成'

# withhold()











class Column(BaseHandler):
    def get(self):
        sql = "select  count(id) as tcount,date_format(addtime,'%Y-%m') as times from invest group by times"
        res = db.findall(sql)
        print(res)
        tlist = []
        timelist = []
        for i in res:
            tlist.append(i['tcount'])
            timelist.append(i['times'])
        print(timelist)
        print(tlist)

        self.finish({'code':200,'timelist':timelist,'tlist':tlist})

